package com.jindianjichi.subject.domain.handler.subject;

import com.jindianjichi.basic.entity.SubjectJudge;
import com.jindianjichi.basic.service.SubjectJudgeService;
import com.jindianjichi.subject.common.enums.IsDeletedEnum;
import com.jindianjichi.subject.common.enums.SubjectInfoTypeEnum;
import com.jindianjichi.subject.domain.convert.JudgeSubjectConverter;
import com.jindianjichi.subject.domain.entity.SubjectAnswerBo;
import com.jindianjichi.subject.domain.entity.SubjectInfoBo;
import com.jindianjichi.subject.domain.entity.SubjectOptionBo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@Slf4j
public class SubjectJudgeHandler implements SubjectInfoHandler{
    @Resource
    SubjectJudgeService subjectJudgeService;
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.JUDGE;
    }

    @Override
    public void add(SubjectInfoBo subjectInfoBo) {
        List<SubjectAnswerBo> optionList = subjectInfoBo.getOptionList();
        SubjectAnswerBo subjectAnswerBo = optionList.get(0);
        SubjectJudge subjectJudge=new SubjectJudge();
        subjectJudge.setSubjectId(subjectInfoBo.getId());
        subjectJudge.setIsCorrect(subjectAnswerBo.getIsCorrect());
        subjectJudge.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
        subjectJudgeService.insert(subjectJudge);
    }

    @Override
    public SubjectOptionBo queryById(Long subjectId) {
        SubjectJudge subjectJudge=new SubjectJudge();
        subjectJudge.setSubjectId(subjectId);
        List<SubjectJudge> judgeList = subjectJudgeService.queryByCondition(subjectJudge);
        SubjectOptionBo subjectOptionBo=new SubjectOptionBo();
        List<SubjectAnswerBo> answerBos = JudgeSubjectConverter.instance.convertAnswerBolist(judgeList);
        subjectOptionBo.setOptionList(answerBos);
        return subjectOptionBo;
    }
}
